<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;

class Order extends Base
{

    public function index()
    {
        //
    }

    function list() {
        return $this->fetch();
    }

    public function list1()
    {
        return $this->fetch();
    }
    public function add()
    {
        if (request()->isPost()) {
            // dump(input());die;
            $data = input();
            $datas = [
                'name' => $data['name'],
                'score' => $data['score'],
                'stock' => $data['stock'],
                'img' => $data['img'],
                'createtime' => date('Y-m-d H:i:s', time()),
                'updatetime' => date('Y-m-d H:i:s', time()),
            ];
            if (isset($data['status']) && isset($data['status']) == 'on') {
                $datas['status'] = 1;
            } else {
                $datas['status'] = 0;
            }

            $result = Db::table('goods')->where('name', $data['name'])->find();
            if ($result) {
                //重复数据
                return show(0, '数据重复', []);
            } else {
                $res = Db::table('goods')->insert($datas);
                if ($res) {
                    return show(1, '提交成功', []);
                }
            }

        }

        return $this->fetch();
    }

    //商品列表

    public function goodslist()
    {

        return $this->fetch();
    }

    // 商品添加

    public function goodsadd()
    {
        return $this->fetch();
    }

    public function getGoodlist()
    {
        $res = Db::table('goods')->where('status', '<>', 0)->select();
        $count = Db::table('goods')->where('status', '<>', 0)->count();

        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $res,
        ];

        echo json_encode($data);
    }

    //单条数据编辑

    public function sgoods()
    {

        $datas = input();
        $id = $datas['id'];
        $res = Db::table('goods')->where('id', $id)->where('status', 1)->find();
        $this->assign('res', $res);
        return $this->fetch();
    }

    //修改

    public function edit()
    {
        $data = input();
        halt($data);
        $id = $data['id'];
        $datas = [
            'name' => $data['name'],
            'score' => $data['score'],
            'stock' => $data['stock'],
            'img' => $data['img'],
            // 'createtime' => date('Y-m-d H:i:s', time()),
            'updatetime' => date('Y-m-d H:i:s', time()),
        ];
        if (isset($data['status']) && isset($data['status']) == 'on') {
            $datas['status'] = 1;
        } else {
            $datas['status'] = 0;
        }

        $result = Db::table('goods')->where('name', $data['name'])->find();
        if ($result) {
            //重复数据
            return show(0, '数据重复', []);
        } else {
            $res = Db::table('goods')->where('id', $id)->update($datas);
            if ($res) {
                return show(1, '修改成功', []);
            }
        }

    }

    public function del()
    {
        $data = input();
        $id = $data['id'];
        $res = Db::table('goods')->where('id', $id)->delete();
        if ($res) {
            return show(1, '删除成功', []);
        } else {
            return show(0, "删除失败", []);
        }

    }

}
